

export const Encrypt = (word, keyStr, ivStr) =>{
    let key = CryptoJS.enc.Utf8.parse('bjbcsoft20220923')
    let iv = CryptoJS.enc.Utf8.parse('bjbcsoft20220923')
    if (keyStr){
        key = CryptoJS.enc.Utf8.parse(keyStr)
        iv = CryptoJS.enc.Utf8.parse(ivStr)
    }
    const srcs = CryptoJS.enc.Utf8.parse(word)
    let encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    })
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext)
}
/**
 * AES 解密 ：字符串 key iv  返回base64
 *
 */
export const Decrypt = (word, keyStr, ivStr) =>{
    let key = CryptoJS.enc.Utf8.parse('bjbcsoft20220923')
    let iv = CryptoJS.enc.Utf8.parse('bjbcsoft20220923')
    if (keyStr){
        key = CryptoJS.enc.Utf8.parse(keyStr)
        iv = CryptoJS.enc.Utf8.parse(ivStr)
    }
    const base64 = CryptoJS.enc.Base64.parse(word)
    const src = CryptoJS.enc.Base64.stringify(base64)
    let decrypt = CryptoJS.AES.decrypt(src, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    })
    let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    return decryptedStr.toString()
}
export const deepClone = (source)=>{
    if (!source && typeof source !== 'object'){
        throw new Error('error arguments deepClone')
    }
    let targetObj
    try {
        Object.keys(source).forEach(keys => {
            if (source[keys] && typeof source[keys] === 'object'){
                targetObj[keys] = deepClone(source[keys])
            } else {
                targetObj[keys] = source[keys]
            }
        })
    } catch (error){
        targetObj = JSON.parse(JSON.stringify(source))
    }
    return targetObj
}
export const getFileSize = (fileByte) =>{
    let fileSize = 0
    if (fileByte < 1048576){
        fileSize = (fileByte / 1024).toFixed(2) + 'KB'
    } else if (fileByte == 1048576){
        fileSize = '1MB'
    } else if (fileByte > 1048576 && fileByte < 1073741824){
        fileSize = (fileByte / (1024 * 1024)).toFixed(2) + 'MB'
    } else if (fileByte == 1073741824){
        fileSize = '1GB'
    } else if (fileByte > 1073741824 && fileByte < 1099511627776){
        fileSize = (fileByte / (1024 * 1024 * 1024)).toFixed(2) + 'GB'
    } else {
        fileSize = '文件超过1TB'
    }
    return fileSize
}
/**
     * 生成formData对象
     * * @param {data} object
     * @return {FormData}
     */
export const formDataInit = (data) =>{
    const datas = new FormData()
    for (let k in data){
        if (typeof data[k] == 'string'){//前后去空格
            data[k] = data[k].replace(/^\s+/, '').replace(/\s+$/, '')
        }
        if (data[k] == 'null' || data[k] === null){
            datas.append[k] = ''
        } else if (k != 'file' && k != 'udfFile' && typeof data[k] == 'object'){
            datas.append(k, JSON.stringify(data[k]))
        } else if(k == 'file' && data[k].constructor == Array){
            data[k].forEach(item => {
                datas.append(k, item)
            })
        }else {
            datas.append(k, data[k] === 0 ? 0 : data[k] === false ? false : data[k] ? data[k] : '')
        }
    }
    return datas
}
//组合get参数
export const setUrlParam = (url, data) =>{
    if(!data){
        return url
    }
    let mid = url
    if (Object.keys(data).length > 0){
        mid = url + '?'
        for (let k in data){
            if (typeof data[k] == 'string'){
                if (data[k] == '%'){
                    data[k] = '------null------'
                }
                data[k] = data[k].replace(/^\s+/, '').replace(/\s+$/, '')
            }
            mid = mid + k + '=' + encodeURIComponent(data[k] === 0 ? 0 : data[k] === false ? false : data[k] ? data[k] : '') + '&'
        }
    }
    return mid
}
export const downFileIframe = (url, data, type = 'GET') =>{//文件下载通过iframe实现
    let iframe = document.createElement('iframe')
    const name = createHash()
    iframe.name = name
    iframe.style.position = 'fixed'
    iframe.style.left = '10000px'
    document.body.appendChild(iframe)
    let form = document.createElement('form')
    // form.action = url
    if(url.indexOf('?') > -1){
        form.action = url + '&accessToken=' + storageGet('token')
    }else{
        form.action = url + '?accessToken=' + storageGet('token')
    }
    form.method = type
    if (data){
        if(!data.accessToken){
            data.accessToken = storageGet('token')
        }
        for (let k in data){
            const input = document.createElement('input')
            input.name = k
            input.value = data[k]
            form.appendChild(input)
        }
    }
    document.body.appendChild(form)
    form.target = name
    form.submit()
    document.body.removeChild(form)
    setTimeout(()=>{
        document.body.removeChild(iframe)
    }, 1000 * 60 * 60)
}
/**
 * 生产hash 默认16位
 * @param {String|Nulber} source
 * @returns {String}
 */
export const createHash = (hashLength = 16) =>{
    let ar = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
    let hs = []
    let hl = Number(hashLength)
    let al = ar.length
    for (let i = 0; i < hl; i++){
        hs.push(ar[Math.floor(Math.random() * al)])
    }
    return hs.join('')
}

//存session
export const storageSet = (name, value) =>{
    localStorage.setItem(name, JSON.stringify(value))
}
//取session
export const storageGet = (name) =>{
    let getData = localStorage.getItem(name)
    if (getData){
        let obj = null
        try {
            obj = JSON.parse(getData)
        } catch (e){
            obj = getData
        }
        return obj
    }
    return ''
}
//清空session
export const storageClear = () =>{
    localStorage.clear()
}

//比较是否有交集
export const intersect = (array1, array2) => {
    let set1 = new Set(array1)
    let set2 = new Set(array2)
    let intersection = new Set([...set1].filter(x => set2.has(x)))
    return [...intersection]
}